package com.easa.dao;

import com.easa.entity.Laboratorymsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
@Transactional
public class LaboratorymsgDaoImpl implements LaboratorymsgDao{

    @Autowired
    private JdbcTemplate jdbcTemplate;


    public List<Laboratorymsg> findAllLaboratorymsg(){
        String sql="SELECT * FROM laboratorymsg WHERE lstatus = 0";
        List<Laboratorymsg> list;
        try {
            list =  jdbcTemplate.query(sql, new BeanPropertyRowMapper(Laboratorymsg.class));
        }catch (Exception e){
            System.out.println(e);
            list = null;
        }
        return list;
    }

    //查询获取实验室信息（后台管理）
    public List<Laboratorymsg> getLaboratoryMsg(String lname){

        StringBuilder sql = new StringBuilder("select * from laboratorymsg ");

        if(lname != null && !lname.equals("")){
            sql.append("where lname like '%"+lname+"%' or lintroduce like '%"+lname+"%' or lresponsible like '%"+lname+"%'");
        }

        List<Laboratorymsg> list;
        try {
            list =  jdbcTemplate.query(String.valueOf(sql), new Object[]{}, new BeanPropertyRowMapper(Laboratorymsg.class));
        }catch (Exception e){
            System.out.println(e);
            list = null;
        }
        return list;
    }

    //通过lid查询实验室信息
    public Laboratorymsg getLaboratoryByLid(int lid){
        String sql="SELECT * FROM laboratorymsg WHERE lid = ?";
        Laboratorymsg laboratorymsg;
        try {
            laboratorymsg = jdbcTemplate.queryForObject(sql, new Object[]{lid}, new BeanPropertyRowMapper<Laboratorymsg>(Laboratorymsg.class));
        }catch (Exception e){
            System.out.println(e);
            laboratorymsg = null;
        }
        return laboratorymsg;
    }

    //更新实验室信息（后台管理）
    public boolean updateLaboratory(Laboratorymsg laboratorymsg){
        String sql = "Update laboratorymsg set lname = ?,lintroduce = ?,lresponsible = ?,lstatus = ?,lcapacity = ? where lid=?";
        Boolean isSuccess;
        try {
            jdbcTemplate.update(sql, new Object[]{laboratorymsg.getLname(), laboratorymsg.getLintroduce(),laboratorymsg.getLresponsible(),laboratorymsg.getLstatus(),laboratorymsg.getLcapacity(),laboratorymsg.getLid()});
            isSuccess = true;
        }catch (Exception e){
            System.out.println(e);
            isSuccess = false;
        }
        return isSuccess;
    }

    //新增实验室信息
    public boolean addLaboratory(Laboratorymsg laboratorymsg){

        String sql = "INSERT INTO laboratorymsg(lid, lname, lintroduce, lresponsible, lcapacity,lstatus) VALUES(?,?,?,?,?,?)";
        int maxId = getMaxId();
        try {
            jdbcTemplate.update(sql, new Object[]{maxId+1, laboratorymsg.getLname(),
            laboratorymsg.getLintroduce(),laboratorymsg.getLresponsible(),
            laboratorymsg.getLcapacity(),laboratorymsg.getLstatus()});
        }catch (Exception e){
            System.out.println(e);
            return false;
        }
        return true;
    }

    //获取最大id
    private int getMaxId() {
        String sql = "SELECT max(lid) FROM laboratorymsg";
        Integer maxId;
        try {
            maxId = jdbcTemplate.queryForObject(sql, java.lang.Integer.class);
        }catch (Exception e){
            System.out.println(e);
            maxId = 0;
        }
        return maxId;
    }
}
